package com.mach.platform.sprider.dev;

import cn.hutool.core.util.HexUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;

public class UU {
	/**
	 * 真实值转逻辑值
	 * @param realNum
	 * @return
	 */

	public static Double realValue2LogicValue
		(Double realNum,
		 Double realStart, Double realEnd,
		 Double logStart,Double logEnd) {

		Double lz = realEnd - realStart;
		Double dt = logEnd - logStart;


		Double result = logStart;

		result = (realNum - realStart ) / lz * dt + logStart ;
		return result;
	}
	public static Double logiceValue2RealValue
			(Double logicNum,
			 Double realStart, Double realEnd,
			 Double logStart,Double logEnd) {

		Double lz = realEnd - realStart;
		Double dt = logEnd - logStart;


		Double result = null;

		result = (logicNum - logStart )* lz / dt + realStart ;
		return result;
	}

	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static byte[] setCrc16Btye(byte[] data) {
		byte[] result = new byte[data.length + 2];
		cp(data, result);
		
		byte[] crc = getCrc16Btye(data);
		result[result.length - 2] = crc[0];
		result[result.length - 1] = crc[1];
		
		return result;		
	}
	
	public static int getCrc16(byte[] arr_buff) {  
        int len = arr_buff.length;  
          
        //Ԥ�� 1 �� 16 λ�ļĴ���Ϊʮ������FFFF, �ƴ˼Ĵ���Ϊ CRC�Ĵ�����  
        int crc = 0xFFFF;  
        int i, j;  
        for (i = 0; i < len; i++) {  
            //�ѵ�һ�� 8 λ���������� �� 16 λ�� CRC�Ĵ����ĵ� 8 λ�����, �ѽ������ CRC�Ĵ���  
            crc = ((crc & 0xFF00) | (crc & 0x00FF) ^ (arr_buff[i] & 0xFF));  
            for (j = 0; j < 8; j++) {  
                //�� CRC �Ĵ�������������һλ( ����λ)�� 0 ����λ, ��������ƺ���Ƴ�λ  
                if ((crc & 0x0001) > 0) {  
                    //����Ƴ�λΪ 1, CRC�Ĵ��������ʽA001�������  
                    crc = crc >> 1;  
                    crc = crc ^ 0xA001;  
                } else  
                    //����Ƴ�λΪ 0,�ٴ�����һλ  
                    crc = crc >> 1;  
            }  
        }  
        return crc;  
    } 
	
	public static byte[] getCrc16Btye(byte[] arr_buff) {  
		byte[] data = new byte[2];
		
		int crc = getCrc16(arr_buff);
		
		data[0] = (byte) (crc % 256);
		data[1] = (byte) (crc / 256);
		
		return data;
		
	}
	
	public static void cp(byte[] src, byte[] target){
		for(int i = 0; i < src.length; ++i) {
			byte d = src[i];
			target[i] = d;
		}
	}
	/*
	 * ��intת��2���ֽڵ����ݣ�indain��ʽ
	 */
	public static byte[] int2Bs(Integer num) {
		byte[] result = new byte[2];
		
		result[0] = (byte)(num / 256);
		result[1] = (byte)(num % 256);		
		
		return result;
	}
	
	synchronized public static void printData(byte[] data) {
		if(null==data){
			System.out.println();
			return;
		}
		
		for(int i = 0; i < data.length; ++i) {
			byte d = data[i];
			System.out.print(String.format("%02X",d) + ",");
		}
		System.out.println();
	}

    public static void sleep(long i) {
		try {
			Thread.sleep(i);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

    public static Object bytes2Value(String collDataType, byte[] databuf) {
		Object result = null;
		if("float".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			result = ByteUtil.getFloat(databuf);
		}
		if("DINT".equals(collDataType)){
			String hexStr = HexUtil.encodeHexStr(databuf);
			Integer firtstResult = Integer.parseUnsignedInt(hexStr,16);
			// 原数据除以10
			result = new BigDecimal(Double.parseDouble(firtstResult.toString())/10).setScale(1, RoundingMode.HALF_UP).doubleValue();
		}
		if("Real".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			result = ByteUtil.getFloat(databuf);
		}
		if("BOOL".equals(collDataType) && databuf.length > 0){
			result = new Byte(databuf[0]);
		}
		if("floatXES".equals(collDataType)){
			ByteUtil.reveIn2Byte(databuf, 0);
			ByteUtil.reveIn2Byte(databuf, 2);
			result = ByteUtil.getFloat(databuf);
		}
		if("int".equals(collDataType)){
			databuf = ByteUtil.reverse2Byte(databuf);
			databuf = ByteUtil.reverse(databuf);
			result = ByteUtil.getInt(databuf);
		}
		if("INT".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			result = ByteUtil.getShort(databuf);
		}
		if("DWORD".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			result = ByteUtil.getShort(databuf);
		}
		if("int/3600".equals(collDataType)){
			databuf = ByteUtil.reverse2Byte(databuf);
			databuf = ByteUtil.reverse(databuf);
			int resultInt = ByteUtil.getInt(databuf);
			result = resultInt / 3600;
		}
		if("long".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			result = ByteUtil.getLong(databuf);
		}
		if("short".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			result = ByteUtil.getShort(databuf);
		}
		if("short/1000".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			Short rs = ByteUtil.getShort(databuf);
			result = rs * 1.0 / 1000.0;
		}
		if("short/100".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			Short rs = ByteUtil.getShort(databuf);
			result = rs *1.0 / 100.0;
		}
		if("short*300/1000".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			Short rs = ByteUtil.getShort(databuf);
			result = rs * 1.0 * 300.0 / 1000.0;
		}
		if("short/10".equals(collDataType)){
			databuf = ByteUtil.reverse(databuf);
			Short rs = ByteUtil.getShort(databuf);
			result = rs / 10;
		}


		if(collDataType != null && collDataType.startsWith("mA420")){
			String[] dts = collDataType.split("_");
			if(dts.length >= 2 && dts[1] != null){

				String[] startAndEnd = dts[1].split("~");
				if(startAndEnd.length >= 2 && startAndEnd[0] != null && startAndEnd[1] != null){

					int start = Integer.parseInt(startAndEnd[0]);
					int end = Integer.parseInt(startAndEnd[1]);

					databuf = ByteUtil.reverse(databuf);
					Short rs = ByteUtil.getShort(databuf);
					float value = rs * 1.0f/1000.0f;
					//result = result ;
					if(value < 4.0) value = 4.0f;
					result = (value-4.0f)/16.0f*(end - start) + start;
					//result = result; //debug
				}
			}else{
			}

		}

		return result;
    }
}
